package com.airticketbooking.controller.user;

import com.airticketbooking.common.domain.R;
import com.airticketbooking.common.utils.UserContext;
import com.airticketbooking.dto.OrderDTO;
import com.airticketbooking.po.Order;
import com.airticketbooking.vo.OrderVO;
import com.airticketbooking.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Mr.J
 * @version 1.0.0
 * @date 2024/11/11
 * @desc
 */
@RequestMapping("air/user/order")
@RestController
@Api(tags = "用户端订单相关接口")
@RequiredArgsConstructor
@Slf4j
public class UserOrderController {

	private final OrderService orderService;

	//	根据订单号查询订单详情
	@GetMapping("/getByOrderId/{orderId}")
	@ApiOperation(value = "根据订单号查询订单详情", notes = "根据订单号查询订单详情")
	public R<OrderVO> getByOrderId(@PathVariable Integer orderId) {
		log.info("查询订单详情");

		OrderVO orderVO = orderService.getOrderVOById(orderId);

		return R.ok(orderVO);
	}

	//     根据状态查询订单列表
	@GetMapping("/getByStatus")
	@ApiOperation(value = "根据状态查询订单列表", notes = "根据状态查询订单列表")
	public R<List<Order>> getOrderListByStatus(Integer status) {

		OrderDTO orderDTO = new OrderDTO();
		orderDTO.setStatus(status);
		orderDTO.setUserId(UserContext.getUser().intValue());

		List<Order> orders = orderService.querylist(orderDTO);

		return R.ok(orders);

	}

	//	下单
	@PostMapping("/create")
	@ApiOperation(value = "下单", notes = "下单")
	public R<String> createOrder(@RequestParam List<Integer> ticketIds) {
		log.info("下单");


		orderService.createOrder(ticketIds);

		return R.ok("下单成功");
	}

	@GetMapping("/confirmOrder")
	@ApiOperation(value = "选择折扣类型,确认下单", notes = "选择折扣类型,确认下单")
	public R<String> confirmOrder(@RequestParam Integer orderId,
								  @RequestParam Integer discountTypeId,
								  @RequestParam(required = false) Integer mortgagePoint) {
		log.info("选择折扣类型,确认下单");

		if (mortgagePoint != null) {
			// 调用包含抵扣积分的确认下单逻辑
			orderService.confirmOrder(orderId, discountTypeId, mortgagePoint);
		} else {
			// 调用普通的确认下单逻辑
			orderService.confirmOrder(orderId, discountTypeId);
		}

		return R.ok("确认下单成功" + (mortgagePoint != null ? "，抵扣积分数额为 " + mortgagePoint : ""));
	}


	//改签
	@GetMapping("/modify")
	@ApiOperation(value = "改签", notes = "改签")
	public R<String> modifyOrder( @RequestParam Integer orderId,
								  @RequestParam Integer oldticketId,
								  @RequestParam(required = false) Integer newticketId) {
		log.info("改签");

		try {
			orderService.modifyOrder(orderId, oldticketId, newticketId);
			return R.ok("改签成功");

		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}

	//	取消订单
	@GetMapping("/cancel")
	@ApiOperation(value = "取消订单", notes = "取消订单")
	public R<String> cancelOrder(@RequestParam Integer orderId,
								 @RequestParam Integer ticketId) {
		log.info("取消订单");

		try {
			orderService.cancelOrder(orderId, ticketId);

			return R.ok("取消订单成功");

		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}


}
